package com.itheima.reggie.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.domain.ShoppingCart;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.utils.UserThreadLocal;
import com.itheima.reggie.vo.R;
import com.sun.prism.impl.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 清理购物车
     *
     * @return
     */
    @Override
    public R cleanCart() {
        //从表里查出某一个字段  this.listObjs(WrapperLambdaQuery(table)
        // .eq(),Object::toString);
        return R.success(this.remove(Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(ShoppingCart::getUserId, UserThreadLocal.get().getId())));
    }

    /**
     * 查看购物车
     */
    @Override
    public R listShopping() {

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //查看对应用户的对应的购物车
        wrapper.eq(ShoppingCart::getUserId, UserThreadLocal.get().getId());
        //排序
        wrapper.orderByAsc(ShoppingCart::getCreateTime);

        return R.success(shoppingCartService.list(wrapper));
    }

    /**
     * 添加购物车
     */
    @Override
    public R addShopping(ShoppingCart shoppingCart) {
        //先设置用户id,指定当前是哪个用户的购物车数据  因为前端没有传这个id给我们,但是这个id又非常重要（数据库这个字段不能为null）,
        // 所以要想办法获取到,我们在用户登录的时候就已经保存了用户的id
        /*Long userId = UserThreadLocal.get().getId();
        shoppingCart.setUserId(userId);*/
        Long userId = UserThreadLocal.get().getId();
        shoppingCart.setUserId(userId);

        //获取购物车菜品id
       /* Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);*/
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        // 比对当前购物车的userID和登录的userId
        wrapper.eq(ShoppingCart::getUserId, userId);

        //判断是否为菜品
        if (dishId != null) {
            //添加到购物车的是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
       /* //判断是否为菜品
         if (dishId != null) {
        //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
        //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }*/

        //查询当前菜品是否或者是套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        /*ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);*/
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //如果已经存在,就在原来的数量基础上加一
        if (one != null) {
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //不存在，则数量默认为1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
        /*
        //如果已经存在,就在原来的数量基础上加一
        if (cartServiceOne != null) {
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
        //如果不存在,则添加到购物车，数量默认是1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);*/

    }
}
